package cn.net.wanji.utils;

import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson.JSON;
import org.springframework.util.Assert;

import java.io.File;
import java.util.*;

/**
 * @author guodejun
 * @createTime 2022/11/8 14:22
 * @description
 */
public class Test2 {

//    public static
//    void main(String[] args) throws InterruptedException {
//
//        // 接收文件路径
//        String basePath = args[0];
//        // 读取文件并进行分组
//        String[] directoryFileNames = FileUtil.file(basePath).list();
//        Map<Integer, List<File>> fileData = new HashMap<>();
//        List<Double> fileSizes = new ArrayList<>();
//
//        if (directoryFileNames == null) {
//            return;
//        }
//
//        for (int i = 0; i < directoryFileNames.length; i++) {
//            String directoryFileName = directoryFileNames[i];
//            File file0 = new File(basePath + File.separator + directoryFileName + File.separator + "0.jpeg");
//            File file1 = new File(basePath + File.separator + directoryFileName + File.separator + "1.jpeg");
//            File file2 = new File(basePath + File.separator + directoryFileName + File.separator + "2.jpeg");
//            File file3 = new File(basePath + File.separator + directoryFileName + File.separator + "3.jpeg");
//            File file4 = new File(basePath + File.separator + directoryFileName + File.separator + "4.mp4");
//            List<File> files = Arrays.asList(file0, file1, file2, file3, file4);
//            fileData.put(i, files);
//            long sumLength = 0;
//            for (File file : files) {
//                sumLength += file.length();
//            }
//            fileSizes.add((double) sumLength / 1024 / 1024);
//        }
//
//        // 循环延迟发送请求
//        int index = 0;
//        while (true) {
//            int key = index % fileData.size();
//            Map<String, String> mapParams = new HashMap<>();
//            String recordId = ImageUtils.getRandomString(35);
//            mapParams.put("recordId", recordId);
//            mapParams.put("fileTypes", "0,1,2,3,4");
//            List<File> files = fileData.get(key);
//
//            long l = System.currentTimeMillis();
//            String upload = HttpUtils.upload("http://127.0.0.1:9092/receive/uploadFile", files, mapParams);
//            long l1 = System.currentTimeMillis();
//            long time = l1 - l;
//            System.out.println("recordId:" + recordId + " sendFileSize:" + String.format("%.2f", fileSizes.get(key)) + " time:" + time + " res:" + upload);
//            Thread.sleep(1000);
//            index++;
//        }
//
//    }

//    public static void main(String[] args) throws InterruptedException {
//
//        Random random = new Random();
//
//        while (true) {
//            int n = Math.abs(random.nextInt(1000));
//            int p = Math.abs(random.nextInt(1000));
//            System.out.println("n " + n + " p " + p );
//            double sqrt = sqrt(n, p);
//            System.out.println("n " + n + " p " + p + " sqrt " + sqrt);
//            int key = index % fileData.size();
//            Map<String, String> mapParams = new HashMap<>();
//            String recordId = ImageUtils.getRandomString(35);
//            mapParams.put("recordId", recordId);
//            mapParams.put("fileTypes", "0,1,2,3,4");
//            List<File> files = fileData.get(key);
//
//            long l = System.currentTimeMillis();
//            String upload = HttpUtils.upload("http://10.102.1.157:9093/receive/uploadFile", files, mapParams);
//            long l1 = System.currentTimeMillis();
//            long time = l1 - l;
//            System.out.println("recordId:" + recordId + " sendFileSize:" + String.format("%.2f", fileSizes.get(key)) + " time:" + time + " res:" + upload);
//            Thread.sleep(1000);
//            index++;
//        }
//
//    }

    private static double sqrt(int n, int p) {
        double lower = 0;
        while (lower * lower < n) {
            lower++;
        }
        --lower;
        int flag = 1;
        int d = 10;
        while (flag <= p + 1) {
            d = 10;
            for (int x = 1; x < flag; x++) {
                d = d * 10;
            }
            double step = 1.d / d;
            for (int i = 0; i < 10; i++) {
                if (lower * lower > n) {
                    break;
                }
                lower = lower + step;
            }
            lower = lower - step;
            flag++;
        }
        double v1 = lower * (d * 10);
        int v = ((int) v1 % 100) / 10;
        double res;
        if (v < 5) {
            res = (lower * d - v) / d;
        } else {
            res = (lower * d - v + 10) / d;
        }
        return res;
    }

}
